package com.ssyt.hiring.easyui;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ssyt.hiring.common.AppServlet;
import com.ssyt.hiring.common.Constants;
import com.ssyt.hiring.common.PageModel2;
import com.ssyt.hiring.common.util.DBUtil;
import com.ssyt.hiring.common.util.FastjsonUtil;
import com.ssyt.hiring.talent.entity.TalentEntity;

public class TestServlet extends AppServlet {

	private static final long serialVersionUID = 1L;

	public TestServlet() {
		super();
	}

	@Override
	protected void service(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding(Constants.ENCODE_UTF8);
		String command = request.getParameter("command");
		if ("login".equals(command)) {
			login(request, response);
		} else if ("listMenu".equals(command)) {
			listMenu(request, response);
		} else if ("listTalent".equals(command)) {
			listTalent(request, response);
		} else if ("addTalent".equals(command)) {
			addTalent(request, response);
		} else if ("getTalent".equals(command)) {
			getTalent(request, response);
		} else if ("updateTalent".equals(command)) {
			updateTalent(request, response);
		} else if ("deleteTalent".equals(command)) {
			deleteTalent(request, response);
		}
	}

	private void login(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String code = request.getParameter("code");
		String password = request.getParameter("password");

		if ("zs".equals(code) && "123456".equals(password)) {
			response.getWriter().print(1);
		}
	}

	private void listMenu(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		int id = 0;
		String idStr = request.getParameter("id");
		if (idStr != null) {
			id = Integer.parseInt(idStr);
		}

		List<MenuEntity> menuList = DBUtil.executeQuery(MenuEntity.class,
				"SELECT * FROM menu WHERE pid=?", id);

		print(response, FastjsonUtil.toJson(menuList));
	}

	private void listTalent(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		int page = Integer.parseInt(request.getParameter("page"));
		int rows = Integer.parseInt(request.getParameter("rows"));
		String sort = request.getParameter("sort");
		String order = request.getParameter("order");

		// List<TalentEntity> talentList =
		// DBUtil.executeQuery(TalentEntity.class,
		// "SELECT * FROM talent ORDER BY " + sort + " " + order
		// + " LIMIT " + (page - 1) * rows + "," + rows);
		// List<TalentEntity> talentList2 = DBUtil.executeQuery(
		// TalentEntity.class, "SELECT * FROM talent");

		PageModel2<TalentEntity> talent = DBUtil.executeQueryByPage2(
				TalentEntity.class,
				"SELECT * FROM talent ORDER BY ? ? LIMIT ?,?", sort, order,
				(page - 1) * rows, rows);

		print(response, FastjsonUtil.toJson(talent));
	}

	private void addTalent(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		String code = request.getParameter("code");
		String password = request.getParameter("password");
		String name = request.getParameter("name");
		String phone = request.getParameter("phone");
		String email = request.getParameter("email");
		int age = Integer.parseInt(request.getParameter("age"));
		int gender = Integer.parseInt(request.getParameter("gender"));

		int row = DBUtil
				.executeUpdate(
						"INSERT INTO talent VALUES(NULL, ?, ?, ?, ?, ?, ?, ?, NULL, NULL, NULL)",
						code, password, name, phone, email, age, gender);

		if (row == 1) {
			print(response, 1);
		}
	}

	private void getTalent(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		int id = Integer.parseInt(request.getParameter("id"));

		TalentEntity talent = DBUtil.getUniqueResult(TalentEntity.class,
				"SELECT * FROM talent WHERE id=?", id);

		if (talent != null) {
			print(response, FastjsonUtil.toJson(talent));
		}
	}

	private void updateTalent(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		int id = Integer.parseInt(request.getParameter("id"));
		String code = request.getParameter("code");
		String name = request.getParameter("name");
		String phone = request.getParameter("phone");
		String email = request.getParameter("email");
		int age = Integer.parseInt(request.getParameter("age"));
		int gender = Integer.parseInt(request.getParameter("gender"));

		int row = DBUtil
				.executeUpdate(
						"UPDATE talent SET code=?,name=?,phone=?,email=?,age=?,gender=? WHERE id=?",
						code, name, phone, email, age, gender, id);

		if (row == 1) {
			print(response, 1);
		}
	}

	private void deleteTalent(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		String ids = request.getParameter("ids");
		int row = DBUtil.executeUpdate("DELETE FROM talent WHERE id IN (" + ids
				+ ")");

		if (row >= 1) {
			print(response, 1);
		}
	}
}
